package com.gopay.gass.service.impl;

import com.gopay.gass.constants.NotifyStatus;
import com.gopay.gass.constants.OrderSource;
import com.gopay.gass.constants.RespType;
import com.gopay.gass.dao.GassMainOrderDao;
import com.gopay.gass.dao.GassOrderDtlDao;
import com.gopay.gass.domain.GassMainOrder;
import com.gopay.gass.domain.GassOrderDtl;
import com.gopay.gass.exception.GassServiceException;
import com.gopay.gass.service.GassOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HJY on 2015/3/22.
 */
@Service("gassOrderService")
public class GassOrderServiceImpl implements GassOrderService{

    @Resource(name = "gassMainOrderDao")
    private GassMainOrderDao gassMainOrderDao;

    @Resource(name = "gassOrderDtlDao")
    private GassOrderDtlDao gassOrderDtlDao;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class,RuntimeException.class })
    @Override
    public GassMainOrder saveNewOrder(GassMainOrder main,GassOrderDtl dtl) throws GassServiceException {
        String gassId = null;
        try {
            main.setLastUpdTs(new Date());
            gassId = gassMainOrderDao.getNextId();
            main.setGassId(gassId);
            gassMainOrderDao.save(main);

            dtl.setGassId(gassId);
            dtl.setLastUpdTs(new Date());
            gassOrderDtlDao.save(dtl);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GassServiceException("保存订单失败:"+e.getMessage());
        }
        return main;
    }

    @Override
    public void updateMainOrder(GassMainOrder order){
        gassMainOrderDao.update(order);
    }

    @Override
    public GassMainOrder getMainOrder(String gassId){
        return gassMainOrderDao.find(GassMainOrder.class,gassId);
    }

    @Override
    public List<GassMainOrder> getNotifyOrderList(){
        String hql = "FROM GassMainOrder O WHERE O.respType="+ RespType.Asyn.toString()+" AND O.notifyStatus="+ NotifyStatus.NO.toString()+" ORDER BY O.createdTs ASC ";
        List<GassMainOrder> orderList = gassMainOrderDao.findByHql(hql,new HashMap<String, Object>());
        return orderList;
    }

    @Override
    public GassMainOrder getMainOrder(String gopayOrderId, String orderSource) {
        if(StringUtils.isEmpty(orderSource))
            orderSource = OrderSource.New.toString();

        if(StringUtils.isBlank(gopayOrderId))
            return null;

        String hql = "FROM GassMainOrder O WHERE O.gopayOrderId =:gopayOrderId AND O.orderSource =:orderSource ORDER BY O.createdTs DESC ";
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("gopayOrderId",gopayOrderId);
        params.put("orderSource",orderSource);
        List<GassMainOrder> orderList = gassMainOrderDao.findByHql(hql,params);
        return orderList!=null&&orderList.size()>0 ? orderList.get(0) : null;
    }
}
